package cn.stylefeng.guns.modular.production.productionOrder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.production.productionOrder.entity.MaterialApplyDetail;
import cn.stylefeng.guns.modular.production.productionOrder.entity.OrderBom;
import cn.stylefeng.guns.modular.production.productionOrder.mapper.MaterialAppDetailMapper;
import cn.stylefeng.guns.modular.production.productionOrder.mapper.OrderBomMapper;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.production.productionOrder.entity.Order;
import cn.stylefeng.guns.modular.production.productionOrder.enums.OrderExceptionEnum;
import cn.stylefeng.guns.modular.production.productionOrder.mapper.OrderMapper;
import cn.stylefeng.guns.modular.production.productionOrder.pojo.request.OrderRequest;
import cn.stylefeng.guns.modular.production.productionOrder.service.OrderService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 生产订单表业务实现层
 *
 * @author panl
 * @since 2023/12/15 11:29
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    MaterialAppDetailMapper materialAppDetailMapper;
    @Resource
    OrderBomMapper orderBomMapper;

    @Override
    public void add(OrderRequest orderRequest) {
        Order order = new Order();
        BeanUtil.copyProperties(orderRequest, order);
        this.save(order);
    }

    @Override
    public void del(OrderRequest orderRequest) {
        Order order = this.queryOrder(orderRequest);
        this.removeById(order.getOrderId());
    }

    @Override
    public void close(OrderRequest orderRequest) {
        LambdaUpdateWrapper<Order> lambdaUpdateWrapper = new LambdaUpdateWrapper<Order>();
        lambdaUpdateWrapper.set(Order::getSTATUS, 6);
        lambdaUpdateWrapper.eq(Order::getOrderId, orderRequest.getOrderId());
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public void batchClose(OrderRequest orderRequest) {
        LambdaUpdateWrapper<Order> lambdaUpdateWrapper = new LambdaUpdateWrapper<Order>();
        lambdaUpdateWrapper.set(Order::getSTATUS, 6);
        lambdaUpdateWrapper.in(Order::getOrderId, orderRequest.getOrderId());
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public Map material(OrderRequest orderRequest) {
        Map map = new ConcurrentHashMap();
        LambdaQueryWrapper<OrderBom> lambdaQueryWrappere1 = new LambdaQueryWrapper<OrderBom>();
        lambdaQueryWrappere1.eq(OrderBom::getOrderId, orderRequest.getOrderId());
        map.put("bomlist", orderBomMapper.selectList(lambdaQueryWrappere1));
        LambdaQueryWrapper<MaterialApplyDetail> lambdaQueryWrappere2 = new LambdaQueryWrapper<MaterialApplyDetail>();
        lambdaQueryWrappere2.eq(MaterialApplyDetail::getOrderId, orderRequest.getOrderId());
        map.put("materiallist", materialAppDetailMapper.selectList(lambdaQueryWrappere2));
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(OrderRequest orderRequest) {
        this.removeByIds(orderRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(OrderRequest orderRequest) {
        Order order = this.queryOrder(orderRequest);
        BeanUtil.copyProperties(orderRequest, order);
        this.updateById(order);
    }

    @Override
    public Order detail(OrderRequest orderRequest) {
        return this.queryOrder(orderRequest);
    }

    @Override
    public PageResult<Order> findPage(OrderRequest orderRequest) throws ParseException {
        LambdaQueryWrapper<Order> wrapper = createWrapper(orderRequest);
        Page<Order> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }


    @Override
    public List<Order> findList(OrderRequest orderRequest) throws ParseException {
        LambdaQueryWrapper<Order> wrapper = this.createWrapper(orderRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author panl
     * @since 2023/12/15 11:29
     */
    private Order queryOrder(OrderRequest orderRequest) {
        Order order = this.getById(orderRequest.getOrderId());
        if (ObjectUtil.isEmpty(order)) {
            throw new ServiceException(OrderExceptionEnum.ORDER_NOT_EXISTED);
        }
        return order;
    }

    /**
     * 创建查询wrapper
     *
     * @author panl
     * @since 2023/12/15 11:29
     */
    private LambdaQueryWrapper<Order> createWrapper(OrderRequest orderRequest) throws ParseException {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        BigDecimal orgDataCode = orderRequest.getOrgDataCode();
        String orderId = orderRequest.getOrderId();
        String itemCode = orderRequest.getItemCode();
        String batchCode = orderRequest.getBatchCode();
        String startTime = orderRequest.getStartTime();
        String endTime = orderRequest.getEndTime();
        BigDecimal status = orderRequest.getStatus();
        queryWrapper.eq(ObjectUtil.isNotNull(orgDataCode), Order::getOrgDataCode, orgDataCode);
        queryWrapper.eq(ObjectUtil.isNotEmpty(itemCode), Order::getItemCode, itemCode);
        queryWrapper.eq(ObjectUtil.isNotNull(status), Order::getSTATUS, status);
        queryWrapper.like(ObjectUtil.isNotEmpty(orderId), Order::getOrderId, orderId);
        queryWrapper.like(ObjectUtil.isNotEmpty(batchCode), Order::getBatchCode, batchCode);
        if (ObjectUtil.isNotNull(startTime) && ObjectUtil.isNotNull(endTime)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            queryWrapper.gt(Order::getStartTime, simpleDateFormat.parse(startTime + " 00:00:00"));
            queryWrapper.lt(Order::getEndTime, simpleDateFormat.parse(endTime + " 00:00:00"));
        }
        return queryWrapper;
    }

}
